package com.hzlh.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.hzlh.common.util.PropertiesUtil;
import com.hzlh.core.domain.UploadedFile;
import com.hzlh.core.service.UploadService;
import com.hzlh.oss.ObjectOperations;

@Service
public class FtpAndOssService {

	@Autowired
	@Qualifier("uploadFtpService")
	private UploadService uploadService;
	private static String isoss = PropertiesUtil.getProperties("oss.properties", "oss.isoss");
	/**
	 * 上传文件至阿里云OSS
	 * @param appType
	 * @param file
	 * @return
	 */
	public String uploadLocalFileToOss(String uploadFirstSubDir,String appType,MultipartFile file){
		if("1".equals(isoss)){
		// 上传banner图片
					if(file!=null&&file.getSize()>0){
						//处理上传资料【跳转URL】
						UploadedFile uploadedFile = this.uploadService.uploadFileToUploadDir(uploadFirstSubDir, file);
						String url = this.uploadService.getUploadUrl() + uploadedFile.getReadUrl();
						return url;
					}else{return null;}
		}else{
			
			if ("YUNDAO".equals(appType)) {//2017-3-15云道上传不根据时间格式化文件
				return ObjectOperations.uploadLocalFileToOss(appType ,file,appType);
			}else {
				return ObjectOperations.uploadLocalFileToOss(appType ,file);
			}	
		}
	}
	
	/**
	 * 上传文件至阿里云OSS
	 * @param appType
	 * @param file
	 * @return
	 */
	public UploadedFile uploadLocalFileToOss2(String uploadFirstSubDir,String appType,MultipartFile file){
		if("1".equals(isoss)){
		// 上传banner图片
					if(file!=null&&file.getSize()>0){
						//处理上传资料【跳转URL】
						UploadedFile uploadedFile = this.uploadService.uploadFileToUploadDir(uploadFirstSubDir, file);
						String url = this.uploadService.getUploadUrl() + uploadedFile.getReadUrl();
						uploadedFile.setReadUrl(url);
						uploadedFile.setUrl(url);
						return uploadedFile;
					}else{return null;}
		}else{
			String url =  ObjectOperations.uploadLocalFileToOss(appType ,file);
			UploadedFile uploadedFile =new UploadedFile();
			uploadedFile.setFileUploadedName(url!=null?url.substring(url.lastIndexOf("/")):"");
			uploadedFile.setUrl(url);
			uploadedFile.setFileOriginalName(file.getOriginalFilename());
			return uploadedFile;
		}
	}
}
